<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>统计考试科目的成绩</title>
</head>

<body>
    <input name="btn" type="button" value="统计考试成绩" onclick="count()" />
</body>
<script type="text/javascript">
    function count() {
        var num = prompt("请输入考试科目的数量");
        var sum = 0;
        if (isNaN(num)) {
            alert("输入的不是数字！");
        } else if (num <= 0) {
            alert("输入的考试科目无效！");
        } else {
            for (var i = 1; i <= num; i++) {
                var score = prompt("请输入第" + i + "门科目成绩：");
                if (isNaN(score)) {
                    alert("输入成绩必须是数字！");
                    break;
                } else if (score < 0) {
                    alert("成绩不能是负数！");
                    break;
                } else {
                    sum += parseFloat(score);
                }
            }
        }
        if (sum != 0)
            alert(num + "门科目的总成绩是" + sum);
    }
</script>

</html>